﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using imaginecup.util;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;

namespace Imaginecup.sprite
{
    public class OveraySprite:ISprite
    {
        private String m_TextureName;
        private GraphicsDevice m_GraphicDevice;
        private int m_Width;
        private int m_Height;

        public OveraySprite()
        {
            m_GraphicDevice = GraphicShareData.GetShareData().GetGraphicDevice();//pull the picture from the buffer 

            m_Width = m_GraphicDevice.Viewport.Width;
            m_Height = m_GraphicDevice.Viewport.Height;
        }

        public void SetTextureName(String name)
        {
            m_TextureName = name;
        }

        public void Draw(SpriteBatch spriteBatch, EntityInfo entityInfo)
        {
             int size = (int)(entityInfo.mSize.X * entityInfo.mSize.Y);

            if (size == 0) return ;


            Color[] backBuffer = new Color[size];
            Rectangle rect =   EntityInfo.GetRectangle( entityInfo );

            int startIndex = (int)(entityInfo.mPosition.Y * m_Width + entityInfo.mPosition.X);
            m_GraphicDevice.GetBackBufferData<Color>(rect , backBuffer , startIndex , size);            
            //copy into a texture 
                       

            Texture2D srcTexture = ResourceMan.GetResourceMan().GetTexture(m_TextureName);

            Color[] srcBuffer = new Color[size];
            Rectangle srcRect = new Rectangle(0 , 0 , rect.Width , rect.Height);
            srcTexture.GetData<Color>(0, srcRect, srcBuffer, 0, size);

            CalculateOveray(backBuffer, srcBuffer);

            Texture2D destTexture = new Texture2D(m_GraphicDevice, (int)entityInfo.mSize.X, (int)entityInfo.mSize.Y, false, m_GraphicDevice.PresentationParameters.BackBufferFormat);
            destTexture.SetData(backBuffer);

            spriteBatch.Begin();
            spriteBatch.Draw(destTexture, EntityInfo.GetRectangle(entityInfo), Color.White);
            spriteBatch.End();
            
        }

        private void CalculateOveray(Color[] backBuffer, Color[] srcBuffer)
        {
            for( int i = 0 ; i < backBuffer.Length; i++ )
            {
                Color srcColor = srcBuffer[ i ];
                Color orgColor = backBuffer[ i ];

                backBuffer[i].R = CalculateColor(srcColor.R, orgColor.R);
                backBuffer[i].G = CalculateColor(srcColor.G, orgColor.G);
                backBuffer[i].B = CalculateColor(srcColor.B, orgColor.B);
            }
        }

        private byte CalculateColor(byte src, byte org)
        {
            byte dst = src;     

            if (((src | org) <= 0x7f) || (src >= 0x7f && org <= 0x7f))
            {
                dst = (byte)(((src * org) << 0x01) >> 0x08);
            }
            else if (((src & org) > 0x7f) || ((src ^ org) > 0x7f))
            {
                dst = (byte)(0xFF ^ (((0xFF ^ src) * (0xFF ^ org) << 0x01) >> 0x08));
            }
            return dst;
        }

        public void Release()
        {
         
        }
    }
}
